import {createStore} from 'vuex'

import {ColumnProps,PostProps,testColumnData,testPostsData} from '@/testDate/ColumnDate/index'

interface UserProps {
    isLogin: boolean,
    name?:string,
    id?:number
}
export interface GlobalDataProps {
    columns: ColumnProps[],
    posts: PostProps[],
    user: UserProps
}
const store = createStore <GlobalDataProps>({
    state: {
        columns: testColumnData,
        posts: testPostsData,
        user: {isLogin: false}

    },
    mutations: {
        login(state) {
            console.log('登录：',state);
            
            state.user = {...state.user,isLogin:true,name: 'hmgdec'}
        },
        loginOut(state) {
            state.user = {...state.user,isLogin:false}
        }
    },
    // 状态管理工具的 计算属性，方便在多个页面使用
    getters: {
        getColumnById:(state) => (id:number) => {
          return state.columns.find(c => c.id == id)  
        },

        getPostListById: (state) => (id: number) => {
            return state.posts.filter(c => c.columnId == id)
        }
    }
})

export default store